namespace user{
  const R = require('../common/baseController')
  const {getList, getById, save, update, remove, getFollowing, checkUserExist, follow, unfollow, getFollower} = require('@/service/sys/userService')

  class UserController{
    async getList(ctx:any) {
      const query = ctx.request.body
      const listInfo = await getList(query)
      ctx.body = R.ok(listInfo)
    }
    async getById (ctx:any) {
      const userInfo = await getById(ctx)
      ctx.body = R.ok(userInfo)
    }
    async save(ctx:any) {
      ctx.verifyParams({
        name: {type: 'string', required: true},
        password: {type: 'string', required: true}
      })
      const saveInfo = await save(ctx)
      ctx.body = R.ok(saveInfo)
    }
    async update(ctx:any) {
      ctx.verifyParams({
        id: {type: 'string', required: true},
        name: {type: 'string', required: false},
        password: {type: 'string', required: false},
        avatar: {type: 'string', require: false},
        gender: {type: 'string', require: false},
        headline: {type: 'string', require: false},
        locations: {type: 'array', itemType: 'string', require: false},
        business: {type: 'string'},
        employments: {type: 'array', itemType: 'object', require: false},
        educations: {type: 'array', itemType: 'object', require: false}
      })
      const updateInfo = await update(ctx)
      ctx.body = R.ok(updateInfo)
    }
    async remove(ctx:any) {
      ctx.verifyParams({
        id: {type: 'string', required: true}
      })
      const removeInfo = await remove(ctx)
      ctx.body = R.ok(removeInfo)
    }
    async getFollowing (ctx:any) {
      const followingInfo = await getFollowing(ctx)
      ctx.body = R.ok(followingInfo)
    }
    async checkUserExist(ctx:any, next:any) {
      const user = await checkUserExist(ctx)
      if (!user) {
        ctx.throw(404, '用户不存在')
      }
      await next()
  }
    async follow(ctx:any) {
      const followInfo = await follow(ctx)
      ctx.body = R.ok(followInfo)
    }
    async unfollow(ctx:any) {
      const unfollowInfo = await unfollow(ctx)
      ctx.body = R.ok(unfollowInfo)
    }
    async getFollower(ctx:any){
      const followerInfo = await getFollower(ctx)
      ctx.body = R.ok(followerInfo)
    }
  }

  module.exports = new UserController()
}